# 项目名称：获取闪光读书("https://wp.forchange.cn/")前3页[自定义]书籍信息

# 导入模块
import requests
from bs4 import BeautifulSoup
import csv

url = "https://wp.forchange.cn/page/"

number = 1
# 书籍列表
book_list=[]
while number<=3 :
    url_page = url+str(number)
    # 获取请求网页
    res = requests.get(url_page)
    # print(res.status_code)

    # 解析网页(得到bs-每个结点都是tag对象)
    bs = BeautifulSoup(res.text, "html.parser")
    # 获取书名和网址
    book_info = bs.find_all("a",class_= "post-title")
    # print(book_info)
    for i in book_info:
        # 为每一本书建立字典，存储信息
        dict = {}
        # 获取书名
        book_name = i.text
        # 获取网址
        book_url = i["href"]
        dict["书名"]=book_name

        # 获取请求当前书籍详细信息的网页
        res_page = requests.get(book_url)
        # print(res_page.status_code)
        # 解析当前详情页
        bs_page = BeautifulSoup(res_page.text,"html.parser")
        # 获取当前书籍详细信息
        book_page = bs_page.find("div",class_="res-attrs").find_all("dl")
        for j in book_page:
            key = j.find("dt").text[:-2]
            value = j.find("dd").text
            dict[key] = value

        # 每添加一本书可以打印出来，免的着急
        print(dict)
        # 添加到书架
        book_list.append(dict)
    number+=1

# 为了更方便查看，我们可以将书籍信息写入csv文件，在本地查看
with open("book.csv","w",encoding="utf-8",newline="") as f:
    writer = csv.DictWriter(f,['书名', '作者', '出版社', 'ISBN', '页数', '出版年', '定价'])
    writer.writeheader()
    writer.writerows(book_list)

